<?php

require_once('BaseModel.php');

class Branch_model extends BaseModel {

    function Branch_model()
    {
        $publicdata = array(
			'name',
			'last_update',
			'description',
			'status');
        parent::BaseModel('cr_branches', 'branch_id', $publicdata);
    }

    /**
     * Loads a branch based on his package and name
     * Returns true on success, false otherwise
     */
    function load_branch_from_name($pkgid, $name) {
    	$this->db->where('package', $pkgid);
    	$this->db->where('name', $name);
		return $this->load();
    }
    
    /**
     * Loads a branch based on his id
     * Returns true on success, false otherwise
     */
    function load_branch_from_id($id) {
    	$this->db->where('branch_id', $id);
		return $this->load();
    }
    
    /**
     * Creates a new branch using its infos
     * Returns  true if the branch was successfully created
     * 			an array mapping error messages onto bad fields if it failed 
     */
    function new_branch($infos, $pkgid) {
    	$checkeddata = array();
		$checkeddata['creation_date'] = date('Y-m-d');
		$checkeddata['package'] = $pkgid;
    	
    	//checks whether this name is already in use
    	if ($this->exists(array('package' => $pkgid, 'name' => $infos['name'])))
    		return array('name' => 'Branch already exists for this package');

    	$result = parent::new_data($infos, $checkeddata);
    	if (is_array($result))
    		return $result;
        
        //reloading the branch from his name and package, thus getting his id and complementary infos
        $this->load_branch_from_name($pkgid, $this->data['name']);
        return true;
    }
    
	/**
	 * Stores one branch's information in the database
	 * Returns true if the infos got saved, false if an error occured
	 */
	function save_branch($branchid, $data) {
		return $this->save_data($branchid, $data);
	}
	
	/**
	 * Checks the validity of the infos stored in the $this->data array
	 * Returns true if each field is valid, false otherwise
	 */
	public function fields_are_valid($data) {
		$errormet = false;
		$errors = array();
		foreach ($data as $key => $value) {
			if (isset($value) && strlen($value) == 0) {
				$errors[$key] = $key.' cannot be empty';
				$errormet = true;
			}
		}
		if (isset($data['name']) && strlen($data['name']) > 10) {
			$errors['name'] = 'Name is too long';
			$errormet = true;
		}
		if (isset($data['status']) && !in_array($data['status'], $this->statuses)) {
			$errors['status'] = 'Status is invalid';
			$errormet = true;
		}
		if ($errormet)
			return $errors;
		return TRUE;
	}
    	
    /**
	 * Returns an array of $count branches starting with the $first, all of them attached to $pkgid package
	 * If $count is null, returns all values
	 * This array is sorted on the $sorted column
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_branches($pkgid, $first = null, $count = null, $match = null, $sorted = 'name') {
		$this->db->where('package', $pkgid);
		return $this->get_data($first, $count, $sorted, $match);
	}
	
    /**
     * Returns the total number of branch
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_branches_count($match = null) {
		return $this->get_data_count($match);
	}
	
	/**
	 * Returns whether the user $user_id is responsible for this branch
	 */
	function is_responsible($user_id, $bch_id) {
		$this->db->select('resp_usr');
		$this->db->join('cr_branches', 'cr_branches.package = cr_packages.pkg_id');
		$this->db->where('branch_id', $bch_id);
		$query = $this->db->get('cr_packages');
		$array = $query->row_array();
		return $user_id == $array['resp_usr'];
	}
}

?>
